package com.itbaiwei.unitday01;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.itbaiwei.unitday01.view.MyListView;

import java.util.List;

/**
 * Class: MyAdapter
 * Description:
 * Author: haijun.xu
 * Date: 2025/5/28 15:17
 */
public class MyAdapter extends PagerAdapter {

    private Context mContext;
    private List<String> datas;
    private List<String> data_lists;
    private LayoutInflater layoutInflater;

    public MyAdapter(Context mContext, List<String> datas, List<String> data_lists) {
        this.mContext = mContext;
        this.datas = datas;
        this.data_lists = data_lists;
        this.layoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view == o;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        //super.destroyItem(container, position, object);
        container.removeView((View) object);
    }


    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View view;
        if (position == 0) {
            view = layoutInflater.inflate(R.layout.vp_layout, null);
            MyListView listView = view.findViewById(R.id.lv);
            listView.setAdapter(new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1, data_lists));
            container.addView(view);
        } else {

            view = layoutInflater.inflate(R.layout.vp_layout_textview, null);
            TextView tv = view.findViewById(R.id.tv);
            tv.setText(datas.get(position));
            container.addView(view);
        }
        return view;
    }
}
